{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    '电影名称':['California Man','He\\'s Not Rellay into Dudes','Beautiful Woman','Kevin Longblade','Robo Slayer 3000','Amped II','?'],\n",
    "    '打斗镜头':[3,2,1,101,99,98,18],\n",
    "    '接吻镜头':[104,100,81,10,5,2,90],\n",
    "    '电影类型':['爱情片','爱情片','爱情片','动作片','动作片','动作片','未知']\n",
    "}\n",
    "df=pd.DataFrame(data)\n",
    "df.iloc[:,1:3]\n",
    "new_data=[18,90]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def knn(shuju,yangben,k):\n",
    "\n",
    "    dist=((yangben.iloc[:,1:3] - shuju)**2).sum(axis=1)**0.5\n",
    "    dist_f = pd.DataFrame({\n",
    "        'dist':dist,\n",
    "        'label':df.iloc[:,-1]\n",
    "    })\n",
    "\n",
    "    dist_s =dist_f.sort_values(by='dist').iloc[:k]\n",
    "    re = dist_s.value_counts('label')\n",
    "\n",
    "    return re.index[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'爱情片'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn(new_data,df,5)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a6dc62afd8b03c17538a9dfce2fcb18f62cec380cc7b77050462a64b7e4e4814"
  },
  "kernelspec": {
   "display_name": "Python 3.8.0 32-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
